<template>
	<om-formsub-outer :index="index" :checkedit="checkedit">
		<om-autoform ref="autoform" :initConfigData="autoformdata" :initValue="initValue"></om-autoform>
	</om-formsub-outer>
</template>

<script>
	import {buildSubFormData, subOnLoad, changeEditor,configSelectData} from '../../../static/js/formUtil.js';
	import { ignoreHtml,ptFloat,minusF,getDateStr} from '@/static/js/common.js';
	import urlUtil from '@/static/js/urlUtil.js';
	export default {
		provide() {
			return {
				formPage: this
			};
		},
		data() {
			return buildSubFormData({
				dataCacheKey:"hr-u-entry-data-info-title31-data-cache",
				itemIdsStr:''
			})
		},
		methods: {
			onSelectItem(item){
				let _that = this
				this.om.request('tlApproval/getHandlePosList',{itemId:item.id}).then(res=>{
					//if(res instanceof Array) {
					if(Object.prototype.toString.call(res)==='[object Array]'){
						_that.$refs.autoform.changeEditor("title37", "handlePosId", {
							data:configSelectData(res,"id","text")
						})
						if(res.length == 1){
							_that.$refs.autoform.initSelectIndex("title37", "handlePosId", (res.length == 1?res[0].id:""))
							_that.onSelectHandlePos(res[0])
						} else {
							_that.$refs.autoform.initSelectIndex("title37", "handlePosId")
						}
					} else {
						_that.om.toast(res.msg)
					}
				})
			},
			clearItem(){
				_that.$refs.autoform.clear("title37", "itemId")
				_that.$refs.autoform.clear("title37", "handlePosId")
				_that.$refs.autoform.changeEditor("title37", "handlePosId", {
					data:[]
				})
				_that.$refs.autoform.clear("title37", "handleUserId")
				_that.$refs.autoform.changeEditor("title37", "handleUserId", {
					data:[]
				})
			},
			onSelectHandlePos(item){
				let _that = this
				urlUtil.promise(urlUtil.userByPosComboboxUrl(item.id), function(res){
					//if(res instanceof Array){
					if(Object.prototype.toString.call(res)==='[object Array]'){
						_that.$refs.autoform.changeEditor("title37", "handleUserId", {
							data:configSelectData(res,"id","username")
						})
						if(res.length == 1){
							_that.$refs.autoform.initSelectIndex("title37", "handleUserId", (res.length == 1?res[0].id:""))
						} else {
							_that.$refs.autoform.initSelectIndex("title37", "handleUserId")
						}
					}else{
						_that.om.toast(res.msg)
					}
				})
			},
			clearHandlePos(){
				_that.$refs.autoform.clear("title37", "handlePosId")
				_that.$refs.autoform.clear("title37", "handleUserId")
				_that.$refs.autoform.changeEditor("title37", "handleUserId", {
					data:[]
				})
			},
			initLoad(){
				let _that = this
				this.om.request('hrCommon/getItemCombobox',{
					posId:_that.posId,
					itemIdsStr: this.itemIdsStr,
					busCode: 'HRS0903'
				}).then(res=>{
					//if(res instanceof Array){
					if(Object.prototype.toString.call(res)==='[object Array]'){
						_that.$refs.autoform.changeEditor("title37", "itemId", {
							data:configSelectData(res,"id","itemName")
						})
						_that.$refs.autoform.initSelectIndex("title37", "itemId")
						if(_that.$refs.autoform.formdata.itemId){
							_that.onSelectItem({
								id:_that.$refs.autoform.formdata.itemId
							})
						}
					}else{
						_that.om.toast(res.msg)
					}
				})
			}
		},
		onLoad(e){
			let _that = this
			subOnLoad(_that, e, function() {
				_that.itemIdsStr = e.itemIdsStr
				if(e.configDataCacheKey){
					uni.getStorage({
						key: e.configDataCacheKey,
						success: function(res) {
							//if(res.data && res.data instanceof Array){
							if(res.data && Object.prototype.toString.call(res.data)==='[object Array]'){
								_that.autoformdata = res.data
								Object.assign(_that.initValue, _that.formdata)
								_that.autoformdata[0].order = _that.order
								_that.initLoad()
							}
						}
					});
				}
			})
		},
		onShow() {
			let _that = this
			if(_that.$refs.autoform){
				uni.getStorage({
					key: this.dataCacheKey,
					success: function(res) {
						if(res.data) {
							let d = res.data
							var formdata  = _that.$refs.autoform.formdata
							formdata.dataId = d.id
							formdata.dataName = d.dicname
							uni.removeStorage({
								key: _that.dataCacheKey
							});
						}
					}
				});
			}
		}
	}
</script>

<style>

</style>
